package co.th.bjc.md.dao.impl;

import java.math.BigDecimal;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Projections;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import co.th.bjc.md.dao.CustomerDetailsDao;
import co.th.bjc.md.model.CustomerDetails;

public class CustomerDetailsDaoImpl extends HibernateDaoSupport implements CustomerDetailsDao {

	
	public CustomerDetails getCulCustomerDetails(Double customerDataId) throws Exception{
		CustomerDetails customerDetails = (CustomerDetails) getHibernateTemplate().get(CustomerDetails.class, customerDataId);
		getHibernateTemplate().evict(customerDetails);
		return customerDetails;
	}
	
	public Double insertCustomerDetails(CustomerDetails cus) throws Exception{
		BigDecimal index = (BigDecimal) getHibernateTemplate().save(cus);
		return index.doubleValue();
	}
	
	public void insert(CustomerDetails cus) throws Exception{
		getHibernateTemplate().saveOrUpdate(cus);
	}

	@Override
	public BigDecimal getPK() throws Exception {
		Session session = getHibernateTemplate().getSessionFactory().openSession();
		Criteria criteria = session.createCriteria(CustomerDetails.class)
			    .setProjection(Projections.max("customerDataId"));
		BigDecimal max = (BigDecimal)criteria.uniqueResult();
		return BigDecimal.valueOf(max.intValue() + 1);
		
//		getHibernateTemplate().getSessionFactory().getCurrentSession();
	}
	
}
